﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BolaoDNA.Services;
using BolaoDNA.DomainModel;
using System.Web.Security;
using BolaoDNA.WebMvc.Areas.Admin.ViewModels;
using BolaoDNA.WebMvc.Models;
using System.Web.Routing;

namespace BolaoDNA.WebMvc.Controllers
{
    public class UsuarioCadastradoController : Controller
    {
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }


        private readonly UsuarioCadastradoService usuarioCadastradoService;
        public UsuarioCadastradoController(UsuarioCadastradoService usuarioCadastradoService)
        {
            this.usuarioCadastradoService = usuarioCadastradoService;

        }
        //
        // GET: /UsuarioCadastrado/

        public ActionResult Index()
        {
            return View();
        }
        //
        // GET: /UsuarioCadastrado/SignUp

        public ActionResult SignUp()
        {
            var viewModel = new UsuarioCadastradoViewModel();
            viewModel.Instance = new UsuarioCadastrado();
            return View(viewModel);
        }

        //
        // POST: /UsuarioCadastrado/SignUp

        [HttpPost]
        public ActionResult SignUp(UsuarioCadastradoViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                UsuarioCadastrado usuario = new UsuarioCadastrado();

                usuario.Email = viewModel.Instance.Email;
                usuario.Nome = viewModel.Instance.Nome;

                MembershipCreateStatus status;
                Membership.CreateUser(viewModel.Instance.Email, viewModel.Senha, viewModel.Instance.Email, null, null, true, out status);

                if (status == MembershipCreateStatus.Success)
                {
                    usuarioCadastradoService.Insert(usuario);
                    FormsService.SignIn(viewModel.Instance.Email, true);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("MensagemErro", status.ToString());
                }
            }
            return View(viewModel);
        }

        //
        // GET: /UsuarioCadastrado/Login

        public ActionResult Login()
        {
            var viewModel = new UsuarioCadastradoViewModel();
            viewModel.Instance = new UsuarioCadastrado();
            return View(viewModel);
        }

        //
        // POST: /UsuarioCadastrado/Login

        [HttpPost]
        public ActionResult Login(UsuarioCadastradoViewModel viewModel, String returnUrl)
        {
            if (MembershipService.ValidateUser(viewModel.Instance.Email, viewModel.Senha))
            {
                FormsService.SignIn(viewModel.Instance.Email, true);
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }

            return View(viewModel);
        }
    }
}
